﻿using System;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing.Common;
using ZXing.QrCode;
using ZXing;
using GK.WCS.Open;

namespace Print.printServer {
    public class QRCodePrinter {
        int d = 0;
        public void print(int Code) {
            d = Code;
           PrintDocument pd = new PrintDocument();
            pd.PrintPage += new PrintPageEventHandler(printEventHandler);

            pd.DefaultPageSettings.PrinterSettings.PrinterName = GKtsc.PrinterName;       //打印机名称
                                                                                                     //pd.DefaultPageSettings.Landscape = true;  //设置横向打印，不设置默认是纵向的
            pd.PrintController = new StandardPrintController();
            pd.Print();
        }

        private void printEventHandler(object sender, PrintPageEventArgs e) {
            Font titleFont = new Font("黑体", 11, FontStyle.Bold);//标题字体           
            Font fntTxt = new Font("宋体", 10, FontStyle.Regular);//正文文字         
            Font fntTxt1 = new Font("宋体", 8, FontStyle.Regular);//正文文字           
            Brush brush = new SolidBrush(Color.Black);//画刷           
            Pen pen = new Pen(Color.Black);           //线条颜色         

            try {
                e.Graphics.DrawString("标题name", titleFont, brush, new Point(50, 40));

                Point[] points111 = { new Point(20, 5), new Point(230, 5) };
                e.Graphics.DrawLines(pen, points111); 
                e.Graphics.DrawString(d+"", fntTxt, brush, new Point(10, 2));
            
                for (int i = 3; i < 7; i++) {
                    for (int j = 3; j < 7; j++) {
                        long c = (i * 10L + j) * 100000000L+d;
                        Bitmap bitmap = CreateQRCode(c + ""); 
                        e.Graphics.DrawImage(bitmap, 18 + (i - 3) * 80, 18 + (j - 3) * 80);
                    }
                }
            } catch (Exception ee) {
                MessageBox.Show(ee.Message);
            }
        }


   


        /// <summary>
        /// 二维码方法
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public static Bitmap CreateQRCode(string asset) {
            EncodingOptions options = new QrCodeEncodingOptions {
                DisableECI = true,
                CharacterSet = "UTF-8", //编码
                Width = 100,             //宽度
                Height = 100             //高度
            };
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options = options;
            Bitmap old= writer.Write(asset);

            int margin = 18;
            int w = old.Width - margin * 2;
            int h = old.Height - margin * 2;
            Rectangle cropRect = new Rectangle(margin, margin, w, h);
            Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);
            using (Graphics g = Graphics.FromImage(target)) {
                g.DrawImage(old, new Rectangle(0, 0, target.Width, target.Height),  cropRect,   GraphicsUnit.Pixel);
            }
            return target;
        }
    }
}
